﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;
using BaocaoNPC.common;
using Component;
using CMISLibrary;
//using busBaoCao;
using CMISLibrary.Libraries;
using System.IO;
using Microsoft.VisualBasic;
using BaocaoNPC.Usercontrols.reports;

namespace BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for NPC_INDSTHUTIEN_TAIQUAY.xaml
    /// </summary>
    public partial class NPC_INDSTHUTIEN_TAIQUAY : UserControl
    {
        #region Variables
        bus_TienichNPC.cls_TienichNPC_bus bus = new bus_TienichNPC.cls_TienichNPC_bus();
        public String strMadviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public string strUsername = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "NPC_ATM";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";
        public Int16 i16ThangHT = 0;
        public Int16 i16NamHT = 0;
        public int daysInMonth_HT = 1;
        #endregion
        public NPC_INDSTHUTIEN_TAIQUAY()
        {
            InitializeComponent();
            GET_THANGNAM_HT();
            GetD_DVI_QLY();
            daysInMonth_HT = System.DateTime.DaysInMonth(i16NamHT, i16ThangHT);
            dtpThangHTtim.Text = "0" + i16ThangHT.ToString() + "/" + i16NamHT.ToString();
            dtptungay.Text = "01/" + i16ThangHT.ToString() + "/" + i16NamHT.ToString();
            dtpdenngay.Text = daysInMonth_HT.ToString() + "/" + i16ThangHT.ToString() + "/" + i16NamHT.ToString();

        }
        private void GET_THANGNAM_HT()
        {
            try
            {
                DataSet dsthanglviec = new DataSet();
                //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                //dsthanglviec = bus.get_thang_lviec();
                dsthanglviec = GlobalServices.Service_NPCUS.get_thang_lviec();
                if (dsthanglviec != null && dsthanglviec.Tables.Count > 0)
                {
                    foreach (DataRow dr in dsthanglviec.Tables[0].Rows)
                        if (dr["PHAN_HE"].ToString() == "CN" && dr["MA_DVIQLY"].ToString() == strMadviqly)
                        {
                            i16ThangHT = Convert.ToInt16(dr["THANG"]);
                            i16NamHT = Convert.ToInt16(dr["NAM"]);
                        }
                }
            }
            catch
            {
                i16ThangHT = Convert.ToInt16(DateTime.Now.Month);
                i16NamHT = Convert.ToInt16(DateTime.Now.Year);
            }
        }
        private void GetD_DVI_QLY()
        {
            try
            {
                DataSet dsD_DVI_QLY = new DataSet();

                //cls_TienichNPC_bus cls = new cls_TienichNPC_bus();

                //dsD_DVI_QLY = cls.select_D_DVI_QLY_ALL(strMa_dviqly);

                dsD_DVI_QLY = GlobalServices.Service_NPCUS.select_D_DVI_QLY_ALL(strMadviqly);

                if (dsD_DVI_QLY == null)
                {
                    System.Windows.MessageBox.Show("Không lấy được danh mục đơn vị quản lý.");
                }
                else
                {
                    DataView dv = dsD_DVI_QLY.Tables["D_DVI_QLY"].DefaultView;
                    cboDonVi.ItemsSource = dv;
                    cboDonVi.DisplayMemberPath = "TEN_DVIQLY";
                    cboDonVi.SelectedValuePath = "MA_DVIQLY";
                    cboDonVi.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

        public void showErrorMess(string s)
        {
            Component.Validate.ValidationManager.showMessOnView(this, 5000, s, Component.Validate.ValidationManager.MESSEGER_TYPE.ERROR_MESSAGE);
        }
        public void showSucrMess(string s)
        {
            Component.Validate.ValidationManager.showMessOnView(this, 5000, s, Component.Validate.ValidationManager.MESSEGER_TYPE.SUCCESS_MESSAGE);
        }
        private void btnIn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string strMatngan = strUsername;
                DataSet ds_chamnooffline = new DataSet();
                DateTime? ptungay, pdenngay;
                ptungay = dtptungay.SelectedDate;
                pdenngay = dtpdenngay.SelectedDate;                
                Int16 pthangIn = Convert.ToInt16(dtpThangHTtim.Text.Substring(0, 2)); ;

                Int16 pnamIn = Convert.ToInt16(dtpThangHTtim.Text.Substring(3, 4));
                int ilsu = 0; //0: hien tai, 1: lich su
                if (pthangIn == i16ThangHT && pnamIn == i16NamHT)
                    ilsu = 0;
                else ilsu = 1;
                //ds_chamnooffline = GlobalServices.Service_NPCUS.get_dsthutien_offline(strMadviqly, strMatngan, pthangIn, pnamIn, ptungay, pdenngay);
                //ds_chamnooffline = GlobalServices.Service_NPCUS.get_dschamno(strMadviqly, strMatngan, pthangIn, pnamIn, ilsu, ptungay, pdenngay);
                if (ds_chamnooffline == null || ds_chamnooffline.Tables.Count == 0)
                { showErrorMess("Không có dữ liệu theo điều kiện chọn"); return; }
                if (ds_chamnooffline.Tables["DSTHUTIEN"].Rows.Count > 0)
                {
                    rptDsThutien_Offline rpt = new rptDsThutien_Offline();
                    rpt.SetDataSource(ds_chamnooffline.Tables["DSTHUTIEN"]);
                    rpt.SetParameterValue("ptungay", ptungay);
                    rpt.SetParameterValue("pdenngay", pdenngay);
                    rpt.SetParameterValue("pTendienluc", strTen_dviqly);
                    rpt.SetParameterValue("pTencongty", strTen_dviqly_CT);
                    //ds_chamnooffline.WriteXmlSchema("D:\\Projects\\DSCHAMNO.xsd");
                    UtilReportOMS.RptManager.showReportToWin(this, rpt, strMadviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                }
                else
                { showErrorMess("Không có dữ liệu theo điều kiện chọn"); return; }
            }
            catch(Exception ex)
            { showErrorMess(ex.ToString()); }
        }
    }
}
